package regist;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpUtil;
import regist.aliutil.Config;
import regist.aliutil.MqttSign;
import regist.aliutil.SignatureUtils;

public class AliDeviceQuery {
	private static Logger logger = LoggerFactory.getLogger(AliDeviceQuery.class);
	public  static JSONObject deviceQuery(String deviceName) {
		JSONObject conn_info = new JSONObject();
		String timestamp = LocalDateTimeUtil.format(LocalDateTime.now(ZoneOffset.UTC), "yyyy-MM-dd'T'HH:mm:ss'Z'");
        Map<String, String> map = new HashMap<String, String>();
        // 公共参数
        map.put("Format", "JSON");
        map.put("Version", "2018-01-20");
        map.put("AccessKeyId",  PropertyConfig.getStr("accessKey"));
        map.put("SignatureMethod", "HMAC-SHA1");
        map.put("Timestamp", timestamp);
        map.put("SignatureVersion", "1.0");
        map.put("SignatureNonce", RandomUtil.randomNumbers(14));
        map.put("RegionId", PropertyConfig.getStr("regionId"));
        // 请求参数
        map.put("Action", "QueryDeviceDetail");
        if(StringUtils.isNotEmpty(PropertyConfig.getStr("instanceId")))
        {
        	map.put("IotInstanceId", PropertyConfig.getStr("instanceId"));
        }
        map.put("DeviceName", deviceName);
        map.put("ProductKey", PropertyConfig.getStr("productKey"));
        
        try {
            String signature = SignatureUtils.generate("GET", map, PropertyConfig.getStr("accessKeySecret"));
            map.put("Signature", signature);
            Map<String,Object> httpParamMap = new HashMap<String, Object>(map);
            String result = HttpUtil.get(Config.API_ADDRESS,httpParamMap);
            System.out.println("result====="+result);
            JSONObject queryJson = (JSONObject) JSONObject.parse(result);
            if(queryJson.getBoolean("Success"))
            {
            	JSONObject data = queryJson.getJSONObject("Data");
            	String deviceSecret = data.getString("DeviceSecret");
            	MqttSign sign = new MqttSign();
            	sign.calculate(PropertyConfig.getStr("productKey"), deviceName, deviceSecret);
                conn_info.put("address",PropertyConfig.getStr("productKey")+Config.MQTT_ADDRESS_SUFF);
                conn_info.put("port",Config.MQTT_PORT);
                conn_info.put("productKey", PropertyConfig.getStr("productKey"));
                conn_info.put("clientId",sign.getClientid());
                conn_info.put("userName", sign.getUsername());
                conn_info.put("password", sign.getPassword());
            }            	
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("物联网平台设备查询失败",e);
            return conn_info;
        }
		return conn_info;
	}
	
	public static void main(String[] args) {
		deviceQuery("43166209363903D1");
	}
}
